package weaver.interfaces.zhlx.duss.action.purchasemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 跨工厂调拨流程调用SAP库存查询接口比对库存可用数量
 * @ModifyLog:
 **/
public class WeaKgcdblcSapAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaKgcdblcSapAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            rs.executeQuery("select id,ljbh,pcbh,gc,kw,tskcbs,tskcbh,pglx,ksykcsl,sl,rownum from "
                    + tablename + "_dt1 where mainid=? order by id", mainid);
            int failcount = 0;
            while (rs.next()) {
                String id = Util.null2String(rs.getString("id"));
                double ksykcsl = Util.getDoubleValue(rs.getString("ksykcsl"), 0);
                double sl = Util.getDoubleValue(rs.getString("sl"), 0);
                String rownum = Util.null2String(rs.getString("rownum"));
                String ljbh = Util.null2String(rs.getString("ljbh"));
                String pcbh = Util.null2String(rs.getString("pcbh"));
                String gc = Util.null2String(rs.getString("gc"));
                String kw = Util.null2String(rs.getString("kw"));
                String tskcbs = Util.null2String(rs.getString("tskcbs"));
                String tskcbh = Util.null2String(rs.getString("tskcbh"));
                String pglx = Util.null2String(rs.getString("pglx"));
                JSONObject dataJson = new JSONObject();
                JSONObject headerJson = new JSONObject();
                //物料号
                headerJson.put("MATNR", ljbh);
                ///批次号
                headerJson.put("CHARG", pcbh);
                //工厂
                headerJson.put("WERKS", gc);
                //库存地点
                headerJson.put("LGORT", kw);
                //特殊库存标识
                headerJson.put("SOBKZ", tskcbs);
                //特殊库存编号
                headerJson.put("LIFNR", tskcbh);
                //评估类型
                headerJson.put("BWTAR", pglx);
                dataJson.put("HEADER", headerJson);
                writeLog(">>>>>WeaKgcdblcSapAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = sapUtil.handleOKHttpRequestNotLog("MM088", dataJson,ljbh);
                writeLog(">>>>>WeaKgcdblcSapAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    String output = Util.null2String(resultJson.getString("OUTPUT"));
                    JSONObject outputJson = JSONObject.parseObject(output);
                    String msgType = outputJson.getString("MSG_TYPE");
                    if ("S".equals(msgType)) {
                        String dataStr = outputJson.getString("DATA");
                        JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                        JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                        if (!returnList.isEmpty()) {
                            for (int i = 0; i < returnList.size(); i++) {
                                JSONObject returnJson = returnList.getJSONObject(i);
                                String SOBKZ = Util.null2String(returnJson.getString("SOBKZ"));
                                String CHARG = Util.null2String(returnJson.getString("CHARG"));
                                String WERKS = Util.null2String(returnJson.getString("WERKS"));
                                String LGORT = Util.null2String(returnJson.getString("LGORT"));
                                String BWTAR = Util.null2String(returnJson.getString("BWTAR"));
                                String LIFNR = Util.null2String(returnJson.getString("LIFNR"));
                                String MATNR = Util.null2String(returnJson.getString("MATNR"));
                                String joinRetrunStr = MATNR + CHARG + WERKS + LGORT + SOBKZ + LIFNR + BWTAR;
                                String joinParamStr = ljbh + pcbh + gc + kw + tskcbs + tskcbh + pglx;
                                if (joinRetrunStr.equals(joinParamStr)) {
                                    double fxzkz = Util.getDoubleValue(returnJson.getString("FXZKZ"), 0);
                                    rs.executeUpdate("update " + tablename + "_dt1 set ksykcsl=? where id=?", fxzkz, id);
                                    if (fxzkz < sl) {
                                        failcount++;
                                        message += rownum + "行库存数量不足;";
                                    }
                                }else{
                                    failcount++;
                                    message += rownum + "行查询到结果为空;";
                                }
                            }
                        } else {
                            failcount++;
                            message += rownum + "行查询到结果为空;";
                        }
                    } else {
                        failcount++;
                        message += outputJson.getString("MSG_TEXT");
                    }
                } else {
                    failcount++;
                    message += resultJson.getString("message");
                }
            }
            if (failcount > 0) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaKgcdblcSapAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}